package com.tfjybj.itoo.exam.provider.controller;

import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.exam.entity.TemplateDetailsEntity;
import com.tfjybj.itoo.exam.model.QuestionTypeClassifyModel;
import com.tfjybj.itoo.exam.model.TemplateDetailsModel;
import com.tfjybj.itoo.exam.provider.service.TemplateDetailsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * TemplateDetailsController
 * templateDetails表
 * @author 任晓帅
 * @version 2.0.0
 * @since 2.0.0 2018-11-15 19:40:28
 */
@Api(tags = {"templateDetails表接口"})
@RequestMapping(value = "/templateDetails")
@RestController
public class TemplateDetailsController {

    @Resource
    private TemplateDetailsService templateDetailsService;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model TemplateDetailsModel
     * @return 添加的结果
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody TemplateDetailsModel model) {
		if (StringUtils.isEmpty(model.getTemplateId())){
            return ItooResult.build(ItooResult.FAIL, "templateId为空");
        }
		if (StringUtils.isEmpty(model.getQuestionTypeId())){
            return ItooResult.build(ItooResult.FAIL, "questionTypeId为空");
        }
		if (StringUtils.isEmpty(model.getQuestionClassifyId())){
            return ItooResult.build(ItooResult.FAIL, "questionClassifyId为空");
        }
		if (StringUtils.isEmpty(model.getQuestionNumber())){
            return ItooResult.build(ItooResult.FAIL, "questionNumber为空");
        }
		if (StringUtils.isEmpty(model.getQuestionScore())){
            return ItooResult.build(ItooResult.FAIL, "questionScore为空");
        }
		if (StringUtils.isEmpty(model.getIsExtract())){
            return ItooResult.build(ItooResult.FAIL, "isExtract为空");
        }
		if (StringUtils.isEmpty(model.getDegree())){
            return ItooResult.build(ItooResult.FAIL, "degree为空");
        }
        TemplateDetailsEntity templateDetailsEntity = new TemplateDetailsEntity();
        BeanUtils.copyProperties(model, templateDetailsEntity);
        templateDetailsService.save(templateDetailsEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功          
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        templateDetailsService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiParam( value = "ids",  required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        templateDetailsService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model TemplateDetailsModel
     * @return 修改后的结果
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据id修改templateDetails")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody TemplateDetailsModel model) {
		if (StringUtils.isEmpty(model.getTemplateId())){
            return ItooResult.build(ItooResult.FAIL, "templateId为空");
        }
		if (StringUtils.isEmpty(model.getQuestionTypeId())){
            return ItooResult.build(ItooResult.FAIL, "questionTypeId为空");
        }
		if (StringUtils.isEmpty(model.getQuestionClassifyId())){
            return ItooResult.build(ItooResult.FAIL, "questionClassifyId为空");
        }
		if (StringUtils.isEmpty(model.getQuestionNumber())){
            return ItooResult.build(ItooResult.FAIL, "questionNumber为空");
        }
		if (StringUtils.isEmpty(model.getQuestionScore())){
            return ItooResult.build(ItooResult.FAIL, "questionScore为空");
        }
		if (StringUtils.isEmpty(model.getIsExtract())){
            return ItooResult.build(ItooResult.FAIL, "isExtract为空");
        }
		if (StringUtils.isEmpty(model.getDegree())){
            return ItooResult.build(ItooResult.FAIL, "degree为空");
        }
        TemplateDetailsEntity templateDetailsEntity = new TemplateDetailsEntity();
        BeanUtils.copyProperties(model, templateDetailsEntity);
        templateDetailsService.updateById(templateDetailsEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找TemplateDetails
     *
     * @param id 主键id
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        TemplateDetailsEntity templateDetailsEntity = templateDetailsService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", templateDetailsEntity);
    }

    /**
     * 分页查询所有TemplateDetails
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "分页查询所有TemplateDetails")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo",value = "页码",required = true,example = "1")@PathVariable Integer pageNo, 
								   @ApiParam(name = "pageSize",value = "页数",required = true,example = "10")@PathVariable Integer pageSize) {
        PageInfo<TemplateDetailsEntity> templateDetailss = templateDetailsService.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", templateDetailss);
    }
	
	/**
     * 根据模板id模糊查询templateDetails
     *
     * @param templateId  模板id
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 模糊查询的templateDetails
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据模板id模糊查询templateDetails", notes = "分页根据模板id模糊查询templateDetails")
    @GetMapping(value = "queryTemplateDetailsByLikeTemplateId/{pageNo}/{pageSize}")
    public ItooResult queryTemplateDetailsByLikeTemplateId(@RequestParam(required = false, defaultValue = "") String templateId,
                                                           @ApiParam(value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                                           @ApiParam( value = "记录数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<TemplateDetailsEntity> templateDetailsList = templateDetailsService.queryByLikeTemplateId(templateId, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", templateDetailsList);
    }
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     * 根据templateId查询集合
     *@author 邓立瑶
     * @param templateId
     * @return
     */
    @ApiOperation(value = "根据templateId查询集合")
    @GetMapping(value = "/tempDetail/{templateId}")
    public ItooResult tempDetail(@PathVariable String templateId){
        if (StringUtils.isEmpty(templateId)){
            return ItooResult.build(ItooResult.FAIL,"id不能为空！");
        }
        try {
            List<TemplateDetailsModel> tempList=templateDetailsService.selectTempDetails(templateId);
            return ItooResult.build(ItooResult.SUCCESS,"查询成功！",tempList);
        }catch (Exception e){
            return ItooResult.build(ItooResult.FAIL,"查询失败！");
        }
    }

    /**
     * 根据题型id和课程id查询试题分类
     *
     * @param courseId  课程id
     * @param questionTypeId 题型id
     * @Param operator 用户id
     * @return 查询questionclassify
     * @author 杨海云
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "查询questionclassify")
    @GetMapping(value = "/getQuestionClassify/{questionTypeId}/{courseId}/{operator}")
    public ItooResult getQuestionClassify (
            @PathVariable String questionTypeId,
            @PathVariable String courseId,
            @PathVariable String operator
    ){
        List<QuestionTypeClassifyModel> questionTypeClassifyModelList = templateDetailsService.getQuestionClassify(questionTypeId , courseId,operator);
        return ItooResult.build(ItooResult.SUCCESS,"查询成功",questionTypeClassifyModelList);
    }

}    
